<?php

namespace App\Admin\Controllers\User;

use App\Models\CommonUserTag;
use App\Models\User;
use App\Models\UserTag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class TagController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户绑定标签';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserTag());

        $grid->column('id', ___('table.zy_user_tag.Id'));
        $grid->column('user_id', ___('table.zy_user_tag.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });
        $grid->column('tag_id', ___('table.zy_user_tag.Tag id'))->display(function ($id){
            $tag = CommonUserTag::find($id);
            return $tag?$tag->name:'---';
        });
        $grid->column('create_time', ___('table.zy_user_tag.Create time'));
        $grid->column('update_time', ___('table.zy_user_tag.Update time'));


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserTag::findOrFail($id));

        $show->field('id', ___('table.zy_user_tag.Id'));
        $show->field('user_id', ___('table.zy_user_tag.User id'));
        $show->field('tag_id', ___('table.zy_user_tag.Tag id'));
        $show->field('create_time', ___('table.zy_user_tag.Create time'));
        $show->field('update_time', ___('table.zy_user_tag.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserTag());

        $form->select('user_id', ___('table.zy_user_tag.User id'))->options(function ($id) {
            $user = User::find($id);

            if ($user) {
                return [$user->id => $user->name.'-'.$user->nickname];
            }
        })->ajax('/admin/ajax/user');

        $form->select('tag_id', ___('table.zy_user_tag.Tag id'))->options(function ($id){
            $tag = CommonUserTag::find($id);
            if ($tag) {
                return [$tag->id => $tag->name];
            }
        })->ajax('/admin/ajax/common-tag');

        // $form->number('create_time', ___('table.zy_user_tag.Create time'));
        // $form->number('update_time', ___('table.zy_user_tag.Update time'));
        $form->saving(function (Form $form) {
            $map = [
                'user_id' => $form->user_id,
                'tag_id' => $form->tag_id
            ];

            if (UserTag::where($map)->count()) {
                $error = new MessageBag([
                    'title'   => '数据重复',
                    'message' => '该标签已经绑定',
                ]);

                return back()->with(compact('error'));
            }
        });
        return $form;
    }
}
